package org.jretty.util;

import java.util.ArrayList;
import java.util.List;

import org.jretty.apibase.IMsg;

/**
 * 基础的异常代码定义
 * 
 * 编程时，是否需要区分 客户端异常和
 */
public enum BasicMsg implements IMsg {
    
    // -- 公用的、常见的错误类型
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /** 操作成功 */
    SUCCESS("操作成功", "Success."),
    
    /** 操作失败 */
    FAILED("操作失败", "Failed."),
    
    /** 系统异常 */
    SYSTEM_ERR("系统异常", "System error."),
    
    /** 未定义错误，原因：{} */
    UNDEFINED_ERR("原因：{}", "Cause: {}"),
    
    /** 无效的参数：{} */
    PARAM_INVALID("无效的参数：{}", "Illegal arguments: {}"),
    
    /** 参数不能为空：{} */
    PARAM_EMPTY("参数不能为空：{}", "Arguments can not be empty: {}"),
    
    ;
    
    private String msgZhCN;
    
    private String msgEnUS;
    
    private String code;
    
    
    /** 
     * 构造函数
     */
    private BasicMsg(String msgZhCN, String msgEnUS) {
        this.code = this.name();
        this.msgZhCN = msgZhCN;
        this.msgEnUS = msgEnUS;
    }
    
    /**
     * @return Returns the code.
     */
    @Override
    public String getCode() {
        return code;
    }
    
    @Override
    public String getMsg() {
        if (msgZhCN != null) {
            return msgZhCN;
        }
        if (msgEnUS != null) {
            return msgEnUS;
        }
        return "";
    }
    
    @Override
    public String getMsg(String locale) {
        if ("zh_CN".equals(locale)) {
            return msgZhCN;
        }

        if ("en_US".equals(locale)) {
            return msgEnUS;
        }

        return getMsg();
    }
    
    @Override
    public String toString(){
        return code + "=[" + this.name() + "] " + getMsg();
    }
    
    @Override
    public String toString(String local){
        return code + "=[" + this.name() + "] " + getMsg(local);
    }

    /**
     * 通过枚举<code>code</code>获得枚举
     */
    public static BasicMsg getByCode(String code) {
        for (BasicMsg _enum : values()) {
            if (_enum.getCode().equals(code)) {
                return _enum;
            }
        }
        return null;
    }

    /**
     * 获取全部枚举
     */
    public static List<BasicMsg> getAllEnum() {
        List<BasicMsg> list = new ArrayList<BasicMsg>();
        for (BasicMsg _enum : values()) {
            list.add(_enum);
        }
        return list;
    }

    /**
     * 获取全部枚举值
     */
    public static List<String> getAllEnumCode() {
        List<String> list = new ArrayList<String>();
        for (BasicMsg _enum : values()) {
            list.add(_enum.getCode());
        }
        return list;
    }

}

